package com.wkbb.basic.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.basic.mapper.*;
import com.wkbb.basic.model.*;
import com.wkbb.basic.service.SysRoleService;
import com.wkbb.basic.vo.*;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.OrganizationDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lilin
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends BaseServiceImpl<SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRoleFunctionMapper sysRoleFunctionMapper;

    @Autowired
    private SysRoleFunctionTemplateMapper sysRoleFunctionTemplateMapper;

    @Autowired
    private SysRoleTemplateMapper sysRoleTemplateMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    public void setMapper(SysRoleMapper mapper) {
        super.setMapper(mapper);
    }

    @Override
    public void updateRoleStatus(Map<String, Object> params) {
        sysRoleMapper.updateRoleStatus(params);
    }

    @Override
    public PageInfo<SysRoleUserVo> getRoleUserPageList(Map<String, Object> params, final Integer pageNo, final Integer pageSize) {
        if (pageNo == null || pageSize == null) {
            return null;
        }

        Page<SysRoleUserVo> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<SysRoleUserVo> pageInfo = new PageInfo<SysRoleUserVo>(sysRoleUserMapper.getRoleUserPageList(params));
        pageInfo.setTotal(page.getTotal());

        return pageInfo;
    }

    /**
     * 批量保存角色的用户
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSysRoleUserBatch(Long orgId,Long roleId,List<SysRoleUser> roleUserList) {

        //先删除
        SysRoleUser delSysRoleUser = new SysRoleUser();
        delSysRoleUser.setOrgId(orgId);
        delSysRoleUser.setRoleId(roleId);
        sysRoleUserMapper.delete(delSysRoleUser);

        //批量新增
        if (roleUserList.size() > 0) {
            sysRoleUserMapper.insertBatch(roleUserList);
        }
    }

    /**
     * 删除角色的用户
     */
    @Override
    public void deleteSysRoleUser(SysRoleUser sysRoleUser) {
        sysRoleUserMapper.delete(sysRoleUser);
    }

    /**
     * 查询角色的功能分页列表
     */
    @Override
    public PageInfo<SysRoleFunctionVo> getRoleFunctionPageList(Map<String, Object> params, final Integer pageNo, final Integer pageSize) {
        if (pageNo == null || pageSize == null) {
            return null;
        }

        Page<SysRoleFunctionVo> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<SysRoleFunctionVo> pageInfo = new PageInfo<SysRoleFunctionVo>(sysRoleFunctionMapper.getRoleFunctionPageList(params));
        pageInfo.setTotal(page.getTotal());

        return pageInfo;
    }

    /**
     * 查询角色的功能树形结构
     */
    @Override
    public List<SysRoleFunctionVo> getRoleFunctionTreeList(Map<String, Object> params) {
        return sysRoleFunctionMapper.getRoleFunctionTreeList(params);
    }

    /**
     * 新增角色功能
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSysRoleFunctionBatch(Long orgId, Long roleId, List<SysRoleFunction> roleFunctionList) {
        //批量删除
        SysRoleFunction sysRoleFunction = new SysRoleFunction();
        sysRoleFunction.setOrgId(orgId);
        sysRoleFunction.setRoleId(roleId);
        sysRoleFunctionMapper.delete(sysRoleFunction);

        //再全部新增
        if (roleFunctionList.size() > 0) {
            sysRoleFunctionMapper.insertBatch(roleFunctionList);
        }
    }

    /**
     * 查询菜单树形结构
     */
    @Override
    public List<SysFunctionVo> getUserFunctionTreeList(Map<String, Object> params) {
        return sysRoleFunctionMapper.getUserFunctionTreeList(params);
    }

    /**
     * 查询系统角色树形结构
     */
    @Override
    public List<SysRoleFunctionTemplateVo> getRoleFunctionTemplateTreeList(Map<String, Object> params) {
        return sysRoleFunctionTemplateMapper.getRoleFunctionTemplateTreeList(params);
    }

    /**
     * 同步系统角色模板
     */
    @Transactional
    @Override
    public void syncRoleFunctionTemplate(Long orgId)
    {

        List<SysRoleFunction> sysRoleFunctionList = new ArrayList<SysRoleFunction>();

        //查询组织类型
        Organization organization = organizationMapper.getByPrimaryKey(orgId);

        //查询学校是否已建有角色，没有则复制
        HashMap<String, Object> sysRoleParams = new HashMap<String, Object>();
        sysRoleParams.put("orgId", orgId);
        List<SysRole> sysRoleList = (sysRoleMapper.getListByMapParams(sysRoleParams));
        if(sysRoleList.size()==0) {

            //根据组织类型查询角色
            HashMap<String, Object> sysRoleTemplateParams = new HashMap<String, Object>();
            sysRoleTemplateParams.put("orgType", organization.getOrgType());
            List<SysRoleTemplate> sysRoleTemplateList = sysRoleTemplateMapper.getListByMapParams(sysRoleTemplateParams);
            for (SysRoleTemplate sysRoleTemplate : sysRoleTemplateList) {
                SysRole sysRole = new SysRole();
                sysRole.setApplicationId(1L);
                sysRole.setOrgId(orgId);
                sysRole.setRoleName(sysRoleTemplate.getRoleName());
                sysRole.setRoleType(sysRoleTemplate.getRoleType());
                sysRole.setSystemFlag(true);
                sysRole.setTemplateId(sysRoleTemplate.getId());
                sysRole.setStatus(sysRoleTemplate.getStatus());

                //只能单个插入，因为下面关联需要用到ID
                sysRoleMapper.insert(sysRole);

                //查询模板的权限
                HashMap<String, Object> sysRoleFunctionParams = new HashMap<String, Object>();
                sysRoleFunctionParams.put("templateRoleId", sysRoleTemplate.getId());
                List<SysRoleFunctionTemplate> sysRoleFunctionTemplateList = sysRoleFunctionTemplateMapper.getListByMapParams(sysRoleFunctionParams);
                for(SysRoleFunctionTemplate sysRoleFunctionTemplate : sysRoleFunctionTemplateList)
                {
                    SysRoleFunction sysRoleFunction = new SysRoleFunction();
                    sysRoleFunction.setRoleId(sysRole.getId());
                    sysRoleFunction.setOrgId(orgId);
                    sysRoleFunction.setFunctionId(sysRoleFunctionTemplate.getFunctionId());

                    sysRoleFunctionList.add(sysRoleFunction);
                }
            }

            if(sysRoleFunctionList.size()>0)
            {
                sysRoleFunctionMapper.insertBatch(sysRoleFunctionList);
            }

        }
    }

    /**
     * 新增系统角色功能
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveSysRoleFunctionTemplateBatch(Long roleId, List<SysRoleFunctionTemplate> roleFunctionTemplateList) {
        //批量删除
        SysRoleFunctionTemplate sysRoleFunctionTemplate = new SysRoleFunctionTemplate();
        sysRoleFunctionTemplate.setTemplateRoleId(roleId);
        sysRoleFunctionTemplateMapper.delete(sysRoleFunctionTemplate);

        //再全部新增
        if (roleFunctionTemplateList.size() > 0) {
            sysRoleFunctionTemplateMapper.insertBatch(roleFunctionTemplateList);
        }
    }

    /**
     * 查询用户按钮权限
     * */
    @Override
    public List<SysFunctionVo> getUserMenuButtonList(Map<String, Object> params)
    {
        return sysRoleFunctionMapper.getUserMenuButtonList(params);
    }

    /**
     * 查询用户权限分页列表
     * */
    @Override
    public PageInfo<SysUserRoleVo> getUserRolePageList(Map<String, Object> params, final Integer pageNo, final Integer pageSize)
    {
        if (pageNo == null || pageSize == null) {
            return null;
        }

        Page<SysUserRoleVo> page = PageHelper.startPage(pageNo, pageSize, true);
        PageInfo<SysUserRoleVo> pageInfo = new PageInfo<SysUserRoleVo>(sysRoleUserMapper.getUserRolePageList(params));
        pageInfo.setTotal(page.getTotal());

        return pageInfo;
    }

    /**
     * 查询用户角色类型列表
     * */
    @Override
    public List<SysUserRoleTypeVo> getUserRoleTypeList(Map<String, Object> params)
    {
        return sysRoleUserMapper.getUserRoleTypeList(params);
    }
}
